package net.zhengxinyun.performance.service.indicators;

import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.CountryIndicatorsEntity;
import net.zhengxinyun.performance.entity.CountryScoreEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.CountryScoreFilter;
import net.zhengxinyun.performance.filter.IndexFilter;
import net.zhengxinyun.performance.mapper.Indicators.CountryIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.CountryScoreEntityMapper;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.List;

/**
 * @Auther: hyp
 * @Date: 2018/11/19 11:19
 */
@Service
public class CountryScoreServiceImpl implements CountryScoreService {

    private CountryScoreEntityMapper countryScoreEntityMapper;

    private CountryIndicatorsEntityMapper countryIndicatorsEntityMapper;

    private TokenService tokenService;

    private UserService userService;

    @Autowired
    public CountryScoreServiceImpl(CountryScoreEntityMapper countryScoreEntityMapper, CountryIndicatorsEntityMapper countryIndicatorsEntityMapper, TokenService tokenService, UserService userService) {
        this.countryScoreEntityMapper = countryScoreEntityMapper;
        this.countryIndicatorsEntityMapper = countryIndicatorsEntityMapper;
        this.tokenService = tokenService;
        this.userService = userService;
    }

    @Override
    public int delete(CountryScoreEntity countryScoreEntity) {
        int i = countryScoreEntityMapper.deleteByPrimaryKey(countryScoreEntity.getId());
        return i;
    }

    @Override
    public int update(CountryScoreEntity countryScoreEntity) {
        int i = countryScoreEntityMapper.updateByPrimaryKeySelective(countryScoreEntity);
        return i;
    }

    @Override
    public int insert(CountryScoreEntity countryScoreEntity) {

        int i = countryScoreEntityMapper.insert(countryScoreEntity);
        return i;
    }

    @Override
    public List<CountryScoreFilter> query(CountryScoreFilter countryScoreFilter) {
        List<CountryScoreFilter> list = countryScoreEntityMapper.query(countryScoreFilter);
        return list;
    }

    /**
     * 管理员审核通过
     * @param countryScoreEntity
     * @return
     */
    public int adminPass(CountryScoreEntity countryScoreEntity){
        countryScoreEntity.setAudit("2");
        CountryIndicatorsEntity countryIndicatorsEntity =  countryIndicatorsEntityMapper.selectByPrimaryKey(Long.valueOf(countryScoreEntity.getIndicatorsId()));
       // countryScoreEntity.setFinalScore(Util.getFinalScore(countryScoreEntity.getScore(),countryIndicatorsEntity.getWeight()));
        int i = countryScoreEntityMapper.updateByPrimaryKeySelective(countryScoreEntity);
        return i;
    }

    /**
     * 当二级指标都有最终得分后
     * @param countryScoreEntity
     * @return
     */
    @Transactional
    public String setFirstFinalScore(CountryScoreEntity countryScoreEntity){
        CountryIndicatorsEntity countryIndicatorsEntity = countryIndicatorsEntityMapper.selectByPrimaryKey(Long.valueOf(countryScoreEntity.getIndicatorsId()));
        countryIndicatorsEntity.setMonthTime(countryScoreEntity.getMonthTime());
        if (countryIndicatorsEntityMapper.queryNullFinal(countryIndicatorsEntity) == 0){
            Double finalScore =  countryIndicatorsEntityMapper.queryFirstFinalScore(countryIndicatorsEntity);
            CountryScoreEntity scoreEntity = new CountryScoreEntity();
            scoreEntity.setIndicatorsId(countryIndicatorsEntity.getSuperiorIndexId());
            scoreEntity.setMonthTime(countryScoreEntity.getMonthTime());
            scoreEntity.setDateTime(countryIndicatorsEntity.getDateTime());
            //scoreEntity.setFinalScore(finalScore.toString());
            scoreEntity.setAudit("2");
            int i = countryScoreEntityMapper.insert(scoreEntity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "一级指标得分设置失败";
            }else {
                return "操作成功";
            }
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "还没够";
        }
    }

    /**
     * 首页图表
     * @return
     */
    @Override
    public List<CountryIndicatorsFilter> queryFirstIndicatorsScore(IndexFilter indexFilter) {
        if ("rank".equals(indexFilter.getValue())){
            indexFilter.setRank("1");
        }else if ("score".equals(indexFilter.getValue())){
            indexFilter.setScore("1");
        }
        if (StringUtils.isBlank(indexFilter.getDateTime()) && StringUtils.isBlank(indexFilter.getMonthTime())){
            CountryScoreFilter countryScoreFilter = countryScoreEntityMapper.queryForNewTime();
            if (countryScoreFilter == null){
                return null;
            }
            indexFilter.setDateTime(countryScoreFilter.getDateTime());
            indexFilter.setMonthTime(countryScoreFilter.getMonthTime());
        }
        return countryIndicatorsEntityMapper.indexCountry(indexFilter);
    }

    @Override
    public List<CountryIndicatorsFilter> queryFirstIndicatorsScoreReverse(IndexFilter indexFilter) {
        if ("rank".equals(indexFilter.getValue())){
            indexFilter.setRank("1");
        }else if ("score".equals(indexFilter.getValue())){
            indexFilter.setScore("1");
        }
        if (StringUtils.isBlank(indexFilter.getDateTime()) && StringUtils.isBlank(indexFilter.getMonthTime())){
            CountryScoreFilter countryScoreFilter = countryScoreEntityMapper.queryForNewTime();
            if (countryScoreFilter == null){
                return null;
            }
            indexFilter.setDateTime(countryScoreFilter.getDateTime());
            indexFilter.setMonthTime(countryScoreFilter.getMonthTime());
        }
        return countryIndicatorsEntityMapper.indexCountryReverse(indexFilter);
    }

    @Override
    public Double queryForSumScore(IndexFilter indexFilter) {
        CountryScoreFilter countryScoreFilter = countryScoreEntityMapper.queryForNewTime();
        if (countryScoreFilter == null || StringUtils.isBlank(countryScoreFilter.getDateTime()) || StringUtils.isBlank(countryScoreFilter.getMonthTime())){
            return 0.0;
        }
        indexFilter.setDateTime(countryScoreFilter.getDateTime());
        indexFilter.setMonthTime(countryScoreFilter.getMonthTime());
        Double score = countryScoreEntityMapper.queryForSumScore(indexFilter);
        return score;
    }

    /**
     * 管理员一键通过
     * @param list
     * @return
     */
    @Transactional
    public String adminAllPass(List<CountryScoreEntity> list){
        for (CountryScoreEntity countryScoreEntity:list) {
            //如果批量通过有一条审核状态不对，就所有事务回滚
            if (!"1".equals(countryScoreEntity.getAudit())){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "还未到管理员审核阶段,请稍等";
            }
            CountryIndicatorsEntity countryIndicatorsEntity =  countryIndicatorsEntityMapper.selectByPrimaryKey(Long.valueOf(countryScoreEntity.getIndicatorsId()));
            countryScoreEntity.setAudit("2");
            //countryScoreEntity.setId(Long.valueOf(countryScoreEntity.getAlternateField2()));
           // countryScoreEntity.setAlternateField2(null);
            //countryScoreEntity.setFinalScore(Util.getFinalScore(countryScoreEntity.getScore(),countryIndicatorsEntity.getWeight()));
            int i = countryScoreEntityMapper.updateByPrimaryKeySelective(countryScoreEntity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "操作失败，请重试";
            }
        }
        return "操作成功";
    }

    /**
     * 责任人一键通过
     * @param list
     * @return
     */
    @Override
    @Transactional
    public String responsibleAllPass(List<CountryScoreEntity> list) {
        for (CountryScoreEntity countryScoreEntity:list) {
            //如果批量通过有一条审核状态不对，就所有事务回滚
            if (!"0".equals(countryScoreEntity.getAudit())){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "还未到管理员审核阶段,请稍等";
            }
            countryScoreEntity.setAudit("2");
           // countryScoreEntity.setId(Long.valueOf(countryScoreEntity.getAlternateField2()));
           // countryScoreEntity.setAlternateField2(null);
            int i = countryScoreEntityMapper.updateByPrimaryKeySelective(countryScoreEntity);
            if (i == 0){
                //有一条操作失败就回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "操作失败，请重试";
            }
        }
        return "操作成功";
    }

    /**
     * 责任人审核通过
     * @param countryScoreEntity
     * @return
     */
    @Override
    public int responsiblePass(CountryScoreEntity countryScoreEntity) {
        countryScoreEntity.setAudit("1");
        int i = countryScoreEntityMapper.updateByPrimaryKeySelective(countryScoreEntity);
        return i;
    }

    /**
     * 回退
     * @param countryScoreEntity
     * @return
     */
    @Override
    public int noPass(CountryScoreEntity countryScoreEntity) {
        countryScoreEntity.setAudit("3");
        int i =countryScoreEntityMapper.updateByPrimaryKeySelective(countryScoreEntity);
        return i;
    }



    /*
        查询分数
     */

    @Override
    public CountryScoreEntity selectCityScore(String index, String departcode, String datetime) {
        return countryScoreEntityMapper.selectCityScore(index, departcode, datetime);


    }
}
